function initPage(){
	window.pageSize = 5;
	window.pageNum = 1;
	window.keyWord = "";
}
//分页
function showRolePage(){
	
	//发送ajax请求,获取分页数据
	var pageinfo = getPageDate();
	//给页面加入分页数据
	setPageDate(pageinfo);
	
	//分页导底部航
	setPagefoot(pageinfo);
	
	
}

function getPageDate(){
	var ajaxresult=$.ajax({
		"url":"role/query/page.json",
		"type":"post",
		"data":{
			"pageNum":(window.pageNum == undefined)?1:window.pageNum,
			"pageSize":(window.pageSize == undefined)?5:window.pageSize,
			"keyWord":(window.keyWord == undefined)?"":window.keyWord
		},
		"dataType":"json",
		"async":false	// 为了保证getPageInfo()函数能够在Ajax请求拿到响应后获取PageInfo，需要设置为同步操作
	});
	
	//获取ajax请求结果
	var resultEntity = ajaxresult.responseJSON;
	
	var result = resultEntity.result;//此次根据后台封装json返回数据的实体类来写
	if(result == "SUCCESS"){
		return resultEntity.data;
	}
	if(result == "FAILED"){
		return resultEntity.message;
	}
}

function setPageDate(pageinfo){
/*	<tr>
	  <td>1</td>
	  <td><input type="checkbox"></td>
	  <td>PM - 项目经理</td>
	  <td>
		  <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>
		  <button type="button" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></button>
		  <button type="button" class="btn btn-danger btn-xs"><i class=" glyphicon glyphicon-remove"></i></button>
	  </td>
	</tr>*/
	
	//1、清空表体数据
	$("#tbody").empty();
	//2、获取分页数据列表
	var list = pageinfo.list;//根据后台分页插件pageInfo类来
	if(list == null && list.length <= 0){
		$("#tbody").append("<tr><td colspan='4' stype='text-align:center;'>没有查询到数据！</td></tr>");
	} 
	//3、给页面布局数据
	for(var i=0;i<list.length;i++){
		var role = list[i];
		var td1 = "<td>"+(i+1)+"</td>";
		var td2 = "<td><input class='itembox' type='checkbox' roleid="+role.id+"></td>";//roleid属性是自定义的属性，只有jquery才有
		var td3 = "<td>"+role.name+"</td>";
		var button1 = "<button roleid="+role.id+" type='button' class='btn btn-success btn-xs editAuth'><i class=' glyphicon glyphicon-check'></i></button>";
		var button2 = "<button  roleid="+role.id+" type='button' class='btn btn-primary btn-xs editRole'><i class=' glyphicon glyphicon-pencil'></i></button>";
		var button3 = "<button  roleid="+role.id+" type='button' class='btn btn-danger btn-xs sindel'><i class=' glyphicon glyphicon-remove'></i></button>";
		var td4 = "<td>"+button1+" "+button2+" "+button3+"</td>";
		var tr="<tr>"+td1+td2+td3+td4+"</tr>";
		$("#tbody").append(tr);
	}
	
}

function setPagefoot(pageinfo){
	var paginationProperties = {
			num_edge_entries : 1,			//边缘页数
			num_display_entries : 5,		//主体页数
			callback : pageselectCallback,	//回调函数
			items_per_page : window.pageSize,	//每页显示数据数量，就是pageSize
			current_page : window.pageNum-1,	//当前页页码
			prev_text : "上一页",			//上一页文本
			next_text : "下一页"			//下一页文本
	};
	
	// 显示分页导航条
	$("#Pagination").pagination(pageinfo.total, paginationProperties);
}
//在每一次点击“上一页”、“下一页”、“页码”时执行这个函数跳转页面
function pageselectCallback(pageIndex, jq) {
	
	// pageIndex从0开始，pageNum从1开始
	window.pageNum = pageIndex + 1;
	
	// 重新展示页面
	showRolePage();
	
	return false;
}

function batchRemove(roleidList){
	var jsonlist=JSON.stringify(roleidList)
	alert(jsonlist);
	
	$.ajax({
		"url":"role/batch/remove.json",
		"type":"post",
		"data":jsonlist,
		"contentType":"application/json;charset=UTF-8",
		"dateType":"json",
		"success":function(response){
			var result=response.result;
			if(result=="SUCCESS"){
				alert("删除成功");
				showRolePage();
			}else if(result=="FAILED")
				alert("删除失败");
			else
				alert("momo");
		},
		"error":function(response){
			alert("ajax请求失败");
		}
	});
}
